import { observable, action } from 'mobx';
import API from '../apis/app';

export default class TaskSettingStore {
	@observable appid = '';
	@observable hasInit = false;
	@observable appName;
	@observable hasRight = false;
	@observable taskSettingInfo = {
		name: '',
		createtype: '0',
		triggerway: '0',
		frequencyt: '1',
		weekly: '1',
		datetype: '0',
		dateset: '1',
		timeset: '',
		triggerexpression: '',
		operatorsettype: '0',
		ispathsplit: '0'
	};
	@observable nameError;
	@observable timesetError;
	@observable expressionError;

	@action init(appid) {
		const parent = this.parent();
		const { app } = parent;
		this.appName = app.name;
		this.hasRight = app.rightLevel == 2;
		this.appid = appid;
		API.getAppTaskSetting(this.appid).then(data => {
			this.hasInit = false;
			if (data) {
				this.taskSettingInfo = data;
			}
			this.hasInit = true;
		})
	}

	@action onChange = (key, value) => {
		const info = this.taskSettingInfo;
		if (key === 'createtype' && value === '1') {
			info.triggerway = '0';
		}
		Object.assign(info, { [key]: value });
		this.taskSettingInfo = info;
	}

	@action onSave = () => {
		const params = Object.assign(this.taskSettingInfo, { appid: this.appid });
		return API.saveAppTaskSetting(params);
	}
}